﻿
namespace Mulala.Cavan.Presentation.Mvc.Controller
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.Mvc;
    using Mulala.Cavan.Application.MainBoundedContext.AttachmentModule;
    using Mulala.Cavan.Application.MainBoundedContext.ConsignModule;
    using Mulala.Cavan.Application.MainBoundedContext.MulalaModule;
    using Mulala.Cavan.Application.MainBoundedContext.NannyModule;
    using Mulala.Cavan.Core.Extension;
    using Mulala.Cavan.Core.Helper;
    using Mulala.Cavan.Core.Message;
    using Mulala.Cavan.Domain.MainBoundedContext.AttachmentModule;
    using Mulala.Cavan.Domain.MainBoundedContext.ConsignModule;
    using Mulala.Cavan.Domain.MainBoundedContext.DictionaryModule;
    using Mulala.Cavan.Domain.MainBoundedContext.NannyModule;
    using Mulala.Cavan.Domain.MainBoundedContext.SearchModule;
    using Mulala.Cavan.Domain.MainBoundedContext.Setting;
    using Mulala.Cavan.Presentation.Mvc.ViewModel;
    using Mulala.Cavan.Presentation.Seedwork;
    using Newtonsoft.Json;

    public class NannyController : EntityController<Nanny, NannySearch, INannyViewModel, INannyAppService>, INannyController
    {
        private IAttachmentAppService AttachmentAppService { get; set; }
        private IConsignAppService ConsignAppService { get; set; }

        public NannyController()
        {
            this.Model = ModelFactory.CreateModel<INannyViewModel>();
        }

        public override string ControllerName
        {
            get { return ControllerConst.NANNY; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        [ActionName("photo")]
        public ActionResult GetNannyPhoto(int id)
        {
            return PartialView();
        }

        [ActionName("detail")]
        public ActionResult GetNanny(int id)
        {
            Nanny nanny = this.AppService.GetNannyById(id);

            NannyViewModelConverter.Convert(nanny, this.Model);
            this.SavaHistoryNanny(this.Model);

            return View(this.Model);
        }

        [ActionName("recommendnanny")]
        [ChildActionOnly]
        public ActionResult GetNanniesByRecommend()
        {
            IDictionary<string, bool> orderBy = new Dictionary<string, bool>();
            orderBy.Add("CreateTime", true);
            ConsignSearch consignSearch = ConsignSearchFactory.CreateConsignSearch(this.Model.MasterModel.CurrentUserModel.UserAccountID, null, 1, 1);

            Consign recentConsign = this.ConsignAppService.GetSingleBySearch(consignSearch);

            IList<Nanny> nannies = null;
            if (null != recentConsign)
            {
                NannySearch nannySearch = NannySearchFactory.CreateNannySearch((NannyIdentifier.NannyType)recentConsign.Type.GetHashCode(), recentConsign.Experience, recentConsign.Age, orderBy, 1, 4);
                nannies = this.AppService.GetBySearch(nannySearch).ToList();
            }

            INanniesViewModel nanniesViewModel = ModelFactory.CreateModel<INanniesViewModel>();
            IList<INannyViewModel> nannyViewModels = NannyViewModelConverter.Convert(nannies, false);
            if (null != nannies)
            {
                foreach (NannyViewModel nannyViewModel in nannyViewModels)
                {
                    nannyViewModel.MatchRate = "100%";
                }
            }
            else
            {
                INannyViewModel nannyViewModel = ModelFactory.CreateModel<INannyViewModel>();
                AppMessage message = new AppMessage(this, MessageSeverity.Info, "", "暂无推荐");
                nannyViewModel.Messages.Add(message);
                nannyViewModels.Add(nannyViewModel);
            }
            nanniesViewModel.Nannies = nannyViewModels;

            return PartialView("~/Views/Nanny/_recommendNannyPartial.cshtml", nanniesViewModel);
        }

        [ActionName("greatenanny")]
        [ChildActionOnly]
        public ActionResult GetNannyBySystemRecommend()
        {
            IDictionary<string, bool> orderBy = new Dictionary<string, bool>();
            orderBy.Add("NannyStatus.SystemRecommendTime", true);
            NannySearch nannySearch = NannySearchFactory.CreateNannySearch(true, orderBy, 1, Project.Settings.SystemRecommendNannyAmount);

            IList<Nanny> nannies = this.AppService.GetBySearch(nannySearch).ToList();

            INanniesViewModel nanniesViewModel = ModelFactory.CreateModel<INanniesViewModel>();
            IList<INannyViewModel> nannyViewModels = NannyViewModelConverter.Convert(nannies, false);
            foreach (NannyViewModel nannyViewModel in nannyViewModels)
            {
                nannyViewModel.GreateRate = ((nannyViewModel.Item.SatisfactoryTotal * 100) / nannyViewModel.Item.CommentTotal).ToString() + "%";
            }
            nanniesViewModel.Nannies = nannyViewModels;

            return PartialView("~/Views/Nanny/_greateNannyPartial.cshtml", nanniesViewModel);
        }

        [ActionName("list")]
        public ActionResult GetNanniesByCondition(NannySearch nannySearch)
        {
            long total = 0;
            IList<Nanny> nannies = this.AppService.GetBySearch(nannySearch, out total).ToList();

            INanniesViewModel nanniesViewModel = ModelFactory.CreateModel<INanniesViewModel>();
            IList<INannyViewModel> nannyViewModels = NannyViewModelConverter.Convert(nannies, true);
            nanniesViewModel.Nannies = nannyViewModels;

            return View(nanniesViewModel);
        }

        [ActionName("search")]
        public ActionResult GetNanniesByKeyword()
        {
            string keyword = this.HttpContext.Request["kw"].ToString();
            int currentPage = 1;
            int.TryParse(this.HttpContext.Request["cp"].ToString(), out currentPage);

            long total = 0;
            IList<Nanny> nannies = this.AppService.GetNanniesByKeyword(keyword, currentPage, out total);

            INanniesViewModel nanniesViewModel = ModelFactory.CreateModel<INanniesViewModel>();
            IList<INannyViewModel> nannyViewModels = NannyViewModelConverter.Convert(nannies, true);
            nanniesViewModel.Nannies = nannyViewModels;

            return View(nanniesViewModel);
        }

        [ActionName("byclick")]
        [ChildActionOnly]
        public ActionResult GetNanniesByClick()
        {
            IDictionary<string, bool> orderBy = new Dictionary<string, bool>();
            orderBy.Add("ViewTotal", true);
            NannySearch nannySearch = NannySearchFactory.CreateNannySearch(orderBy, 1, Project.Settings.TopClickNannyAmount);

            IList<Nanny> nannies = this.AppService.GetBySearch(nannySearch).ToList();

            INanniesViewModel nanniesViewModel = ModelFactory.CreateModel<INanniesViewModel>();
            IList<INannyViewModel> nannyViewModels = NannyViewModelConverter.Convert(nannies, false);
            nanniesViewModel.Nannies = nannyViewModels;

            return PartialView(nanniesViewModel);
        }

        [ActionName("history")]
        [ChildActionOnly]
        public ActionResult GetNanniesByHistory()
        {
            IHistoryNanniesViewModel historyNanniesViewModel = ModelFactory.CreateModel<IHistoryNanniesViewModel>();
            if (CookieHelper.IsExsit(Project.Settings.MulalaHistoryCookieName))
            {
                string json = CookieHelper.GetCookie(MulalaAppServiceConst.HISTORYNANNY, Project.Settings.MulalaHistoryCookieName);
                historyNanniesViewModel = JsonConvert.DeserializeObject<HistoryNanniesViewModel>(json);
            }

            return PartialView(historyNanniesViewModel);
        }

        [ActionName("nannies")]
        public ActionResult GetNanniesByCorporation(int id)
        {
            IDictionary<string, bool> orderBy = new Dictionary<string, bool>();
            orderBy.Add("ViewTotal", true);
            NannySearch nannySearch = NannySearchFactory.CreateNannySearch(id, orderBy, 1, Project.Settings.MaxRowsPerPage);

            IList<Nanny> nannies = this.AppService.GetBySearch(nannySearch).ToList();

            INanniesViewModel nanniesViewModel = ModelFactory.CreateModel<INanniesViewModel>();
            IList<INannyViewModel> nannyViewModels = NannyViewModelConverter.Convert(nannies, false);
            nanniesViewModel.Nannies = nannyViewModels;

            return View(nanniesViewModel);
        }

        [ActionName("together")]
        [ChildActionOnly]
        public ActionResult GetNanniesBySameCorporation(int id)
        {
            IDictionary<string, bool> orderBy = new Dictionary<string, bool>();
            orderBy.Add("ViewTotal", true);
            NannySearch nannySearch = NannySearchFactory.CreateNannySearch(id, orderBy, 1, Project.Settings.SameCorporationNannyAmount);

            IList<Nanny> nannies = this.AppService.GetBySearch(nannySearch).ToList();

            INanniesViewModel nanniesViewModel = ModelFactory.CreateModel<INanniesViewModel>();
            IList<INannyViewModel> nannyViewModels = NannyViewModelConverter.Convert(nannies, false);
            nanniesViewModel.Nannies = nannyViewModels;

            return PartialView(nanniesViewModel);
        }

        [ActionName("similar")]
        [ChildActionOnly]
        public ActionResult GetNanniesBySimilar(int id)
        {
            IDictionary<string, bool> orderBy = new Dictionary<string, bool>();
            orderBy.Add("CreateTime", true);

            Nanny nanny = this.AppService.GetById(id);

            IList<Nanny> nannies = null;
            if (null != nanny)
            {
                NannySearch nannySearch = NannySearchFactory.CreateNannySearch(nanny.Type, nanny.Experience, nanny.Age, orderBy, 1, Project.Settings.SimilarNannyAmount);
                nannies = this.AppService.GetBySearch(nannySearch).ToList();
            }

            INanniesViewModel nanniesViewModel = ModelFactory.CreateModel<INanniesViewModel>();
            IList<INannyViewModel> nannyViewModels = NannyViewModelConverter.Convert(nannies, false);
            if (null == nannies)
            {
                INannyViewModel nannyViewModel = ModelFactory.CreateModel<INannyViewModel>();
                AppMessage message = new AppMessage(this, MessageSeverity.Info, "", "暂无相似家政人员");
                nannyViewModel.Messages.Add(message);
                nannyViewModels.Add(nannyViewModel);
            }
            nanniesViewModel.Nannies = nannyViewModels;

            return PartialView(nanniesViewModel);
        }

        [ActionName("introduce")]
        public ActionResult GetNannyForIntroduce(int id)
        {
            Nanny nanny = this.AppService.GetById(id);
            NannyViewModelConverter.Convert(nanny, this.Model);

            return View(this.Model);
        }

        [ActionName("photos")]
        public ActionResult GetNannyPhotos(int id)
        {
            return View();
        }

        [ActionName("goldnanny")]
        [ChildActionOnly]
        public ActionResult GetNannyByGold()
        {
            IDictionary<string, bool> orderBy = new Dictionary<string, bool>();
            orderBy.Add("UserAccount.Score", true);
            NannySearch nannySearch = NannySearchFactory.CreateNannySearch(Project.Settings.GoldMinScore, Project.Settings.GoldMaxScore, NannyIdentifier.NannyType.Nanny, orderBy, 1, Project.Settings.GoldNannyAmount);

            IList<Nanny> nannies = this.AppService.GetBySearch(nannySearch).ToList();

            INanniesViewModel nanniesViewModel = ModelFactory.CreateModel<INanniesViewModel>();
            IList<INannyViewModel> nannyViewModels = NannyViewModelConverter.Convert(nannies, true);
            nanniesViewModel.Nannies = nannyViewModels;

            return PartialView("~/Views/Nanny/_goldNannyPartial.cshtml", nanniesViewModel);
        }

        [ActionName("goldnurse")]
        [ChildActionOnly]
        public ActionResult GetNurseByGold()
        {
            IDictionary<string, bool> orderBy = new Dictionary<string, bool>();
            orderBy.Add("UserAccount.Score", true);
            NannySearch nannySearch = NannySearchFactory.CreateNannySearch(Project.Settings.GoldMinScore, Project.Settings.GoldMaxScore, NannyIdentifier.NannyType.Nurse, orderBy, 1, Project.Settings.GoldNannyAmount);

            IList<Nanny> nannies = this.AppService.GetBySearch(nannySearch).ToList();

            INanniesViewModel nanniesViewModel = ModelFactory.CreateModel<INanniesViewModel>();
            IList<INannyViewModel> nannyViewModels = NannyViewModelConverter.Convert(nannies, true);
            nanniesViewModel.Nannies = nannyViewModels;

            return PartialView("~/Views/Nanny/_goldNursePartial.cshtml", nanniesViewModel);
        }

        private void SavaHistoryNanny(INannyViewModel nanny)
        {
            if (!CookieHelper.IsExsit(Project.Settings.MulalaHistoryCookieName))
            {
                CookieHelper.CreateCookie(Project.Settings.MulalaHistoryCookieName);
                CookieHelper.SetCookie(MulalaAppServiceConst.HISTORYNANNY, string.Empty, Project.Settings.MulalaHistoryCookieName);
            }
            string json = CookieHelper.GetCookie(MulalaAppServiceConst.HISTORYNANNY, Project.Settings.MulalaHistoryCookieName);
            IHistoryNanniesViewModel historyNanniesViewModel = ModelFactory.CreateModel<IHistoryNanniesViewModel>();

            if (json.IsNotEmpty())
            {
                historyNanniesViewModel = JsonConvert.DeserializeObject<HistoryNanniesViewModel>(json);
                if (historyNanniesViewModel.HistoryNannies.Count + 1 > Project.Settings.HistoryNannyAmount)
                {
                    IHistoryNannyViewModel earliestNanny = historyNanniesViewModel.HistoryNannies.OrderByDescending(hn => hn.ClickTime).First();
                    historyNanniesViewModel.HistoryNannies.Remove(earliestNanny);
                }
            }

            IHistoryNannyViewModel newNanny = ModelFactory.CreateModel<IHistoryNannyViewModel>();
            newNanny.ClickTime = DateTime.Now;
            newNanny.Avatar = nanny.Avatar;
            newNanny.Name = nanny.Item.Name;
            newNanny.Type = nanny.Type;

            historyNanniesViewModel.HistoryNannies.Add(newNanny);

            json = JsonConvert.SerializeObject(historyNanniesViewModel);

            CookieHelper.SetCookie(MulalaAppServiceConst.HISTORYNANNY, json, Project.Settings.MulalaHistoryCookieName);
        }
    }
}
